package com.kxmall.web.controller.huishouapp;

import cn.dev33.satoken.annotation.SaIgnore;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.core.controller.BaseAppController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.huishou.domain.HsWasteActivity;
import com.kxmall.huishou.domain.bo.HsActivityJoinBo;
import com.kxmall.huishou.domain.bo.HsWasteActivityBo;
import com.kxmall.huishou.domain.bo.HsWasteNewsBo;
import com.kxmall.huishou.domain.vo.HsActivityJoinVo;
import com.kxmall.huishou.domain.vo.HsWasteActivityVo;
import com.kxmall.huishou.domain.vo.HsWasteNewsVo;
import com.kxmall.huishou.service.IHsActivityJoinService;
import com.kxmall.huishou.service.IHsWasteActivityService;
import com.kxmall.huishou.service.IHsWasteNewsService;
import com.kxmall.user.domain.vo.KxUserVo;
import com.kxmall.user.mapper.KxUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 线下活动报名
 *
 * @author lf
 * @date 2023-12-01
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/activity/app")
public class HsAppActivityController extends BaseAppController {

    private final IHsActivityJoinService iHsActivityJoinService;
    private final IHsWasteActivityService iHsWasteActivityService;

    /**
     * APP查询线下活动信息列表
     */
    @GetMapping("/appList")
    public TableDataInfo<HsWasteActivityVo> appList(HsWasteActivityBo bo, PageQuery pageQuery) {
        if(pageQuery != null){
            pageQuery.setIsAsc("desc");
            pageQuery.setOrderByColumn("create_time");
        }
        return iHsWasteActivityService.queryPageList(bo, pageQuery);
    }


    /**
     * APP查询线下活动详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<HsWasteActivityVo> getAppInfo(@NotNull(message = "主键不能为空")@PathVariable Long id) {
        return R.ok(iHsWasteActivityService.queryById(id));
    }

    /**
     * 新增活动报名记录
     */
    @Log(title = "活动报名记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HsActivityJoinBo bo) {
        Long userId = getAppLoginUser().getUserId();

        HsActivityJoinBo hsActivityJoinBoData = new HsActivityJoinBo();
        hsActivityJoinBoData.setActivityId(bo.getActivityId());
        hsActivityJoinBoData.setUserId(userId);
        List<HsActivityJoinVo> dataList = iHsActivityJoinService.queryList(hsActivityJoinBoData);
        if(dataList != null && dataList.size() >0){
            return R.fail("已经报名成功，无需报名");
        }

        bo.setUserId(userId);

        return toAjax(iHsActivityJoinService.insertByBo(bo));
    }










}
